package com.niiwoo.civet.trade.service.local.loan;

import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.user.dto.response.credit.BasicInfoDTO;
import com.niiwoo.civet.user.dto.response.credit.ContactPersonInfoDTO;
import com.niiwoo.civet.user.dto.response.credit.WorkInfoDTO;
import com.niiwoo.civet.user.enums.TianchengCallbackEventEnum;
import com.niiwoo.tripod.tiancheng.enums.FunctionCodeEnum;
import com.niiwoo.tripod.tiancheng.request.afp.dto.TCBasicInfo;
import com.niiwoo.tripod.tiancheng.request.afp.dto.TCContactPersonInfo;
import com.niiwoo.tripod.tiancheng.request.afp.dto.TCWorkInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 将从user-api dubbo服务中获取到的dto转成天秤模型
 * 
 * @author zhoulinquan
 */
@Slf4j
public class TianchengModelParseService {
	// {1} : webHttpUrl，对应配置web-http-url
	// {2} : TianchengCallbackEventEnum.eventType， 对应请求天秤业务类型
	// {3} : projectId，标的ID
	// {4} : ProjectTypeEnum.value， 产品类型，用于区分不同平台产品对应同一个天秤接口
	// {5} : FunctionCodeEnum.bodyFunctionCode，天秤定义
	public static final String TIANCHENG_RCE_CALLBACK_URL = "%s/tc/v2/%s/%s/%s/callback?functionCode=%s";

	/**
	 * 构建天秤基础信息
	 * 
	 * @param baseInfo
	 * @return
	 */
	public static TCBasicInfo parseBaseInfo(BasicInfoDTO baseInfo) {
		if (baseInfo != null) {
			TCBasicInfo tcBasicInfo = new TCBasicInfo();
			try {
				BeanUtils.copyProperties(baseInfo, tcBasicInfo);
			} catch (Exception e) {
				log.error(e.getMessage(), e);
				return null;
			}

			return tcBasicInfo;
		}

		return null;
	}

	/**
	 * 构建天秤工作信息
	 * 
	 * @param workInfo
	 * @return
	 */
	public static TCWorkInfo parseWorkInfo(WorkInfoDTO workInfo) {
		if (workInfo != null) {
			TCWorkInfo tcWorkInfo = new TCWorkInfo();
			try {
				BeanUtils.copyProperties(workInfo, tcWorkInfo);
			} catch (Exception e) {
				log.error(e.getMessage(), e);
				return null;
			}

			return tcWorkInfo;
		}

		return null;
	}

	/**
	 * 构建天秤联系人
	 * 
	 * @param contacts
	 * @return
	 */
	public static List<TCContactPersonInfo> parseContactPersonInfo(List<ContactPersonInfoDTO> contacts) {
		if (contacts != null && !contacts.isEmpty()) {
			List<TCContactPersonInfo> tcContacts = new ArrayList<>();
			TCContactPersonInfo contact = null;
			for (ContactPersonInfoDTO contactDto : contacts) {
				if (contactDto == null)
					continue;

				contact = new TCContactPersonInfo();

				try {
					BeanUtils.copyProperties(contactDto, contact);
				} catch (Exception e) {
					log.error(e.getMessage(), e);
					return null;
				}

				tcContacts.add(contact);
			}

			return tcContacts.isEmpty() ? null : tcContacts;
		}

		return null;
	}

	/**
	 * 构建天秤回调地址
	 * 
	 * @param webHttpUrl
	 * @param event 
	 * @param projectId 标的ID
	 * @param product 平台产品类型
	 * @param functionCode 天秤functionCode
	 * 
	 * http://172.21.1.179:8080/civet/tc/v2/auditPointFake/11111/10/callback?functionCode=10101
	 * 
	 * @return
	 */
	public static String buildNotifyUrl(String webHttpUrl, TianchengCallbackEventEnum event, String projectId,
			ProjectTypeEnum product, FunctionCodeEnum functionCode) {
		return String.format(TIANCHENG_RCE_CALLBACK_URL, webHttpUrl, event.getEventType(), projectId, product.getValue(),
				functionCode.getHeadFunctionCode());
	}

}
